﻿package controller
{
	
	import Config;
	
	import fl.transitions.*;
	import fl.transitions.easing.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.printing.PrintJob;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	import flash.utils.Endian;
	import flash.utils.Timer;
	
	import model.Level;
	import model.Print;
	import model.Product;
	import model.Question;
	import model.xmlHandler;
	
	import view.Cart;
	import view.Explanation;
	import view.Introduction;
	import view.Kasse;
	import view.ProductWrapper;
	import view.Score;
	import view.Shelf;
	
	public class GameController	extends MovieClip
	{	
		private var shelf:Shelf = null;
		private var productWrapperInst:ProductWrapper = null;
		private var explanation:Explanation = new Explanation();
		private var score:Score = new Score();
		public var xml:xmlHandler;
		private var question:Question;
		private var print:Print = new Print();
		private var cartPosition:int = 1;
		
		private var products:Array = new Array;
		
		private var timer:Timer;
		private var fallingTimer:Timer = new Timer(400, 1);
		private var prodNumber:int = 0;
		private var catchedProducts:int = 0;
		private var mistakes:int = 0;
		private var numberOfProducts:int = Config.RightProductsPerLevel + Config.WrongProductsPerLevel;
		private var wrongMistakes:Array = new Array;
		private var gameEnd:Boolean = false;
		private var paramDictTimer:Dictionary = new Dictionary();
		
		private var positiverSound:Sound = new Sound;
		private var negativerSound:Sound = new Sound;
		private var questionSound:Sound = new Sound;
		private var kasseSound:Sound = new Sound;
		private var squeakSound:Sound = new Sound;
		
		private var soundChannel:SoundChannel;
		
		private var stars:int = 1;
		private var earnedStars:int = 0;
		private var starTweenOne:Tween;
		private var starTweenTwo:Tween;
		
		private var glowScanner:GlowFilter = new GlowFilter(0xFF0000,26,20,20,2,3,false,false);
		private var glowWrong:GlowFilter = new GlowFilter(0xFF0000, 50, 10, 10, 2, 1, true, false);
		private var glowRight:GlowFilter = new GlowFilter(0x00FF00, 50, 20, 20, 2, 1, true, false);
		private var glowPrint:GlowFilter = new GlowFilter(0xFFFFFF, 50, 20, 20, 2, 1, false, false);
		
		private var totalPoints:int = 0;
		public var textField:TextField;
		private var format:TextFormat;
		private var currentLevel:int = 1;
		private var wonMedal:String = "";
		private var pic:int;
		private var starX:int = 0;
		private var starY:int = 0;
		private var button_next:MovieClip;
		private var note:MovieClip;
		private var explanationNote:Boolean = true;
		
		private var cart:Cart = new Cart();
		
		private var repeating:Boolean = false;
		
		public function GameController()
		{
			this.stop(); //stop beim ersten Keyframe	
		
			//make invisible for intro fadingIn effect	
			this.visible = false;			
			
			textField = new TextField();
			textField.x = 700;
			textField.y = 530;
			textField.text = "" + totalPoints;
			textField.visible = false;
			
			format = new TextFormat();
			format.font = "Segoe Print";
			format.size = "34";
			format.color = "#000000";
			textField.setTextFormat(format);  
			this.addChild(textField);
			
			this.positiverSound.load(new URLRequest(Config.OtherSoundPath + "positiv.mp3"));
			this.negativerSound.load(new URLRequest(Config.OtherSoundPath + "negativ.mp3"));
			this.kasseSound.load(new URLRequest(Config.OtherSoundPath + "Kasse.mp3"));
			this.squeakSound.load(new URLRequest(Config.SqueakSound));
			
			xml = new xmlHandler(this);		
			
			this.note = new MovieClip;
			
			var zettel_loader:Loader = new Loader;
			var request:URLRequest = new URLRequest;
			request.url = Config.scriptedPics + "Auswertung.png";
			zettel_loader.load(request);
			this.note.addChild(zettel_loader);
			this.play_btn.enabled = false;
			this.print_btn.enabled = false;
			
		}	
		
		public function playIntroduction():void 
		{		
			var intro:Introduction = new Introduction(this, this.initGameStartAfterIntro);
			intro.fadeIn();			
		}
		
		public function initGameStartAfterIntro():void {
			this.gotoAndStop(5);
			this.loadQuestion();
			
			textField.visible = true;
			//this.callScore();
		}
		
		/**
		 * Läd die aktuelle Frage
		 */
		public function loadQuestion():void
		{
			trace("Current Level:" + currentLevel);
			question = xml.getQuestionToLevel(currentLevel);
			startGame();	
		}
		
		private function repeatQuestion(e:Event):void
		{
			if (!repeating){
				this.soundChannel = this.questionSound.play();
				this.cart_front.face.startSpeakMoving(soundChannel);
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, questionRepeated);
				repeating = true;
			}
			
			
		}
		
		private function questionRepeated(e:Event):void
		{
			repeating = false;
		}
		
		/**
		 * ClickHandler für den "Überspringen"-Button mit Sprung zum nächsten Screen
		 */
		public function startGame():void 
		{
			
			this.questionSound = question.getQuestionSound();
			if(this.questionSound.bytesLoaded == this.questionSound.bytesTotal){
				this.soundChannel = this.questionSound.play();
				this.cart_front.face.startSpeakMoving(soundChannel);
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, startLevel);	
			}else{
				startGame();
			}
		}
		
		/**
		 * Startet das Level, wenn die Frage gestellt wurde
		 */
		private function startLevel(e:Event):void
		{	
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, startLevel);
			this.cart_front.face.stopSpeakMoving(null);
			this.helpBtn.addEventListener(MouseEvent.CLICK, repeatQuestion);
			this.shelf = new Shelf(this.question.getProducts());
			this.addChildAt(this.shelf, Config.ShelfIndexZ);
			this.letProductsFly();
		}
		
		/**
		 * Lässt die Produkte auf das Laufband fliegen und ruft die Behandlung vom Wagen auf
		 */
		private function letProductsFly():void		
		{				
			// flieg Produkt, flieg
			shelf.letNextProductFly(this);
			controllingShelf();
			controlCart();
		}
		
		/**
		 * Timer der Produkte
		 */
		private function controllingShelf():void
		{
			timer = new Timer(3500, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, moveProduct);
			timer.start();
		}
		
		/**
		 * ruft das nächste Produkt auf
		 */
		private function moveProduct(e:TimerEvent):void
		{
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, moveProduct);
			if(prodNumber < shelf.getRemainingProducts()-1){	//numberOfProducts-1){ // wenn nicht alle Produkte vom Regal
				shelf.letNextProductFly(this);
				prodNumber ++;
				if(!gameEnd){
					if(checkGameState()){ //Wenn spiel noch nicht zu ende, lass nächstes Produkt fliegen
						controllingShelf();
					}
				}				
			}		
		}
		
		/**
		 * cartControl
		 * fängt die Tastatureingabe ab um den Wagen zu bewegen
		 */
		private function controlCart():void
		{			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed);	
		}
		
		/**
		 * Eventhandler für die Pfeil-Tasten
		 */
		private function pressed(event:KeyboardEvent):void 
		{
			var tweenTop:Tween;
			var tweenBottom:Tween;
			var diff:int = this.cart_front.x - this.cart_back.x; 
			if (event.keyCode == Keyboard.RIGHT && cartPosition < 3) {
				tweenBottom = new Tween(this.cart_front, "x", null, this.cart_front.x, Config.cartPositions[cartPosition], Config.cartSpeed, true);
				tweenTop = new Tween(this.cart_back, "x", null, this.cart_back.x, Config.cartPositions[cartPosition]-diff, Config.cartSpeed, true);
				this.soundChannel = this.squeakSound.play();
				tweenTop.addEventListener(TweenEvent.MOTION_FINISH, stopSqueak);
				
				cartPosition++;
			}
			if (event.keyCode == Keyboard.LEFT && cartPosition > 1) {
				tweenBottom = new Tween(this.cart_front, "x", null, this.cart_front.x, Config.cartPositions[cartPosition-2], Config.cartSpeed, true);
				tweenTop = new Tween(this.cart_back, "x", null, this.cart_back.x, Config.cartPositions[cartPosition-2]-diff, Config.cartSpeed, true);
				this.soundChannel = this.squeakSound.play();
				tweenTop.addEventListener(TweenEvent.MOTION_FINISH, stopSqueak);
				cartPosition--;
			}
		}
		
		
		private function stopSqueak(e:Event):void
		{
			this.soundChannel.stop();
		}
		
		/**
		 * HitCollider
		 */
		public function testHitting(product:ProductWrapper, productWrap:ProductWrapper):void
		{
			this.productWrapperInst = productWrap;
			if(existsInArray(product)){
				products[searchArray(product)][1] = null;		
			}else{
				products.push(new Array(product, null));
			}
			product.addEventListener(Event.ENTER_FRAME, gotHit);
		}
		
		/**
		 * Hit-Collider, wenn der Wagen getroffen wurde
		 */
		private function gotHit(e:Event):void
		{	
			var index:int = searchArray(ProductWrapper(e.target));
			if(e.target.hitTestObject(this.cart_front)){
				var rightObject:Boolean = this.question.checkProduct(e.target.getProduct());
				if(rightObject){
					if(!gameEnd){
						ProductWrapper(e.target).filters = [glowRight]; 
						this.soundChannel = this.positiverSound.play();
					}
				}else{
					if(!gameEnd){
						this.soundChannel = this.negativerSound.play();
						ProductWrapper(e.target).filters = [glowWrong];
					}
				}			
				products[index][1] = true;
				e.target.removeEventListener(Event.ENTER_FRAME, gotHit);
				if(!gameEnd){
					var tween:Tween = new Tween(e.target, "alpha", null, 1, 0, 1, true);
				}
			}else{
				products[index][1] = false;
			}			
		}
		
		/**
		 * Überprüft den Spielzustand
		 */
		private function checkGameState():Boolean
		{
			var resume:Boolean = true;
			if (mistakes == 3 || catchedProducts == 5){
				gameEnd = true;
				resume = false;
				if(mistakes != 0){
					//Hier wird zur Auswertung gewechselt
					this.gotoAndStop(10);		
				}
				this.removeChild(shelf);
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressed);
			}
			return resume;
		}
		
		/**
		 * Nimme die Bewegung vom Wagen
		 */
		public function removeCartListener(product:ProductWrapper):void
		{
			if(!gameEnd){
				if(checkGameState()){
					if(products[searchArray(product)][1] == false){
						checkFall(product);	
					}else{
						checkCatch(product);
					}
				}
			}
			product.removeEventListener(Event.ENTER_FRAME, gotHit);
		}
		
		/**
		 * Überprüft welches Produkt gefangen wurde
		 */
		private function checkCatch(productWrapper:ProductWrapper):void
		{
			var rightObject:Boolean = this.question.checkProduct(productWrapper.getProduct());
			if(rightObject){
				catchedProducts++;
				this["wagen"+catchedProducts].gotoAndStop(5);
				if (!checkGameState()){					
					resultExplanation();
				}
			}else{
				if(prodNumber < shelf.getRemainingProducts()-1){ //numberOfProducts-1){
					mistakes++;
					if(!arrayDist(productWrapper))
						wrongMistakes.push(productWrapper);
					this["life" + mistakes].gotoAndStop(5);
					if(!checkGameState()){
						resultExplanation();
					}
				}
			}
		}
		
		/**
		 * Überprüft welches Produkt fallen gelassen wurde
		 */		
		private function checkFall(productWrapper:ProductWrapper):void
		{
			var rightObject:Boolean = this.question.checkProduct(productWrapper.getProduct());
			if(rightObject){
				if(!gameEnd){
					productWrapper.filters = [glowWrong];
					this.soundChannel = this.negativerSound.play();
				}
				fallingTimer.delay = 400;
				mistakes ++;
				this["life" + mistakes].gotoAndStop(5);
				if(!arrayDist(productWrapper))
					wrongMistakes.push(productWrapper);
				if(checkGameState()){
					numberOfProducts ++;
					shelf.putItBack(productWrapper, this);					
				}else{
					productWrapper.alpha = 0;
					resultExplanation();
				}			
			}else{
				fallingTimer.delay = 10;
			}
			paramDictTimer[fallingTimer] = productWrapper;
			
			fallingTimer.start();
			fallingTimer.addEventListener(TimerEvent.TIMER_COMPLETE, fallingHandler);
		}
		
		/**
		 * Handler für den Timer, welcher das Produkt stehen lässt
		 */ 
		private function fallingHandler(e:TimerEvent):void 
		{
			var params:Object = paramDictTimer[e.currentTarget];
			productWrapperInst.backToShelf(ProductWrapper(params));
			ProductWrapper(params).filters = null;
		}
		
		/**
		 * Durchsucht das Array und gibt gefundenen Index zurück
		 */ 
		private function searchArray(productWrapper:ProductWrapper):int
		{
			var tmp:int = -1;
			for(var i:int = 0; i < products.length; i++){
				if(products[i][0] == productWrapper){
					tmp = i;
				}
			}			
			return tmp;
		}
		
		/**
		 * Überprüft, das Mistakes nicht doppelt gespeichert werden
		 */
		private function arrayDist(productWrapper:ProductWrapper):Boolean
		{
			var tmp:Boolean = false;
			if (wrongMistakes.length != 0){
				for(var i:int = 0; i < wrongMistakes.length; i++){
					if(wrongMistakes[i] == productWrapper){
						return true;
					}
				}
			}
			return false;
		}
		
		/**
		 * Sucht Wert in einem Array
		 */
		private function existsInArray(productWrapper:ProductWrapper):Boolean{
			for(var i:int = 0; i < products.length; i++){
				if(products[i][0] == productWrapper){
					return true;
				}
			}
			return false;
		}
		
		/**
		 * Ruft die Auswertung auf
		 */
		private function resultExplanation():void
		{
			
			//Load
		//	this.cart_front.visible = false;
		//	this.cart_back.visible = false;
			var tween:Tween = new Tween(this.cart_front, "x", null, this.cart_front.x, 1000, 2, true);
			tween = new Tween(this.cart_back, "x", null, this.cart_back.x, 1000, 2, true);
			this.soundChannel = this.squeakSound.play();
			
			
			tween.addEventListener(TweenEvent.MOTION_FINISH, getNextCart);
		}
		
		private function getNextCart(e:Event):void
		{
			this.soundChannel.stop();
			var tweene = new Tween(this.trans_rect, "alpha", null, 0, 0.5, 2, true);
			this.cart.x = -100;
			this.addChildAt(cart, 15);//numChildren-14  );	
			this.swapChildren(this.cart, this.trans_rect);
			this.cart.moveCartFromCurrentXTo(150, this.startExplanation, true);		
			this.soundChannel = this.squeakSound.play();
			
		}
		
		public function startExplanation(e:Event):void
		{
			this.soundChannel.stop();
			for(var j:int = 0; j < wrongMistakes.length; j++){
				this.addChildAt(this.wrongMistakes[j], numChildren - 1);
			}
			if(mistakes == 3)
				explanation.startExplanation(this.question, this, this.wrongMistakes, mistakes, false, this.cart);
			if(catchedProducts == 5)
				explanation.startExplanation(this.question, this, this.wrongMistakes, mistakes, true, this.cart);
		}
		
		
		public function getScannerGlow():GlowFilter
		{
			return glowScanner;
		}	
		
		public function getPrintGlow():GlowFilter
		{
			return glowPrint;
		}	
		
		/**
		 * Stoppt den Glow-Effekt
		 */
		public function stopGlow(productWrapper:ProductWrapper):void
		{
			productWrapper.filters = null;
		}
		
		public function getGameState():Boolean
		{
			return gameEnd;
		}
		
		public function setPoints(amount:int):void
		{
			this.earnedStars = amount;
			letStarsFly();
		}
		
		/**
		 * Lässt die Sterne von der Kasse bis runter fliegen
		 */
		public function letStarsFly():void
		{
			this.Kasse1.gotoAndStop(2);
			this.Kasse1.gotoAndStop(3);
			this.starX = this["star"+stars].x;
			this.starY = this["star"+stars].y;
			this.soundChannel = this.kasseSound.play();
			starTweenOne = new Tween(this["star"+stars], "alpha", null, 0, 1, 0.1, true);
			starTweenOne = new Tween(this["star"+stars], "x", null, this["star"+stars].x, 275, 0.5, true);
			starTweenOne = new Tween(this["star"+stars], "y", null, this["star"+stars].y, 105, 0.5, true);
			starTweenOne = new Tween(this["star"+stars], "scaleX", null, 0.5, 1.2, 0.5, true);
			starTweenOne = new Tween(this["star"+stars], "scaleY", null, 0.5, 1.2, 0.5, true);	
			starTweenOne.addEventListener(TweenEvent.MOTION_FINISH, flyDown);
			
		}
		
		private function flyDown(e:Event):void
		{
			starTweenOne.removeEventListener(TweenEvent.MOTION_FINISH, flyDown);
			starTweenTwo = new Tween(this["star"+stars], "x", null, this["star"+stars].x, 770, 0.5, true);
			starTweenTwo = new Tween(this["star"+stars], "y", null, this["star"+stars].y, 550, 0.5, true);
			starTweenTwo = new Tween(this["star"+stars], "scaleX", null, 1.2, 0.5, 0.5, true);
			starTweenTwo = new Tween(this["star"+stars], "scaleY", null, 1.2, 0.5, 0.5, true);
			this.Kasse1.gotoAndStop(2);
			this.Kasse1.gotoAndStop(1);
			starTweenTwo.addEventListener(TweenEvent.MOTION_FINISH, nextStar);
			
		}
		
		private function nextStar(e:Event):void
		{
			this["star"+stars].x = this.starX;
			this["star"+stars].y = this.starY;
			this.totalPoints++;
			this.textField.text = "" + totalPoints;
			textField.setTextFormat(format);  
			this["star"+stars].alpha = 0;
			stars++;
			if(earnedStars < stars){
				button_next = new MovieClip;
				button_next.x = 730;
				button_next.y = 235;
				button_next.scaleX = 0.8;
				button_next.scaleY = 0.8;
				button_next.filters = [glowPrint];
				button_next.buttonMode = true;
				button_next.useHandCursor = true;
				
				var loader:Loader = new Loader;
				var request:URLRequest = new URLRequest;
				request.url = Config.scriptedPics + "next_button.png";
				loader.load(request);
				button_next.addChild(loader);
				stage.addChild(button_next);
				button_next.addEventListener(MouseEvent.CLICK, continueGame);		
			}else{
				letStarsFly();
			}
			//earnedStars < stars ? explanation.nextTask() : letStarsFly();
		}
		
		
		private function continueGame(e:Event):void
		{
			button_next.filters = null;
			button_next.removeEventListener(MouseEvent.CLICK, continueGame);
			stage.removeChild(button_next);
			explanation.nextTask();
		}
		
		/**
		 * ruft den Score auf (ENDE)
		*/
		public function callScore():void
		{			
			pic = (Math.floor((Math.random()*4))) + 1;
			for(var i:int = 0; i < wrongMistakes.length; i++){
				this.removeChild(wrongMistakes[i]);
			}
			this.gotoAndStop(15);
			var tween:Tween = new Tween(this.trans_rect, "alpha", null, 0, 0.5, 2, false);
			this.print_btn.addEventListener(MouseEvent.CLICK, buttonPrint);
			this.play_btn.addEventListener(MouseEvent.CLICK, playAgain);
		//	this.swapChildren(this.cart, this.note);				
			var buttons:Array = new Array(this.play_btn, this.print_btn);
			
			this.textField.text = "" + totalPoints;
			textField.setTextFormat(format);  
			if(17 < totalPoints && totalPoints < 21){
				//Goldmedaille
			//	this.medal_gold.alpha = 1;
				this.score.medal(totalPoints, "gold", buttons, this.cart, this);
				this.wonMedal = "gold";
			}else{
				if(14 < totalPoints && totalPoints < 18){
				//	this.medal_silver.alpha = 1;
					this.score.medal(totalPoints, "silber", buttons, this.cart, this);
					this.wonMedal = "silber";
				}else{
					if(11 < totalPoints && totalPoints < 15){
					//	this.medal_bronze.alpha = 1;		
						this.score.medal(totalPoints, "bronze", buttons, this.cart, this);
						this.wonMedal = "bronze";
					}else{
					//	this.medal_gray.alpha = 1;	
						this.wonMedal = "gray";
						if(mistakes == 3){
							this.score.noMedal(totalPoints, this.cart, this, buttons);
						}else{
							this.score.noMedalNotEnoughPoints(totalPoints, this.cart, this, buttons);
						}						
					}
				}
			}
		}
		
		public function showMedal():void
		{
			if(this.wonMedal != ""){
				var medalTween:Tween = new Tween (this["medal_"+this.wonMedal], "alpha", null, 0, 1, 2, true);
			}
		}
		
		/**
		 * Blendet den Erklärungsbildschirm ein
		 */
		public function showNote(x:int, y:int, kind:Boolean):void 
		{ 
			this.explanationNote = kind;
			this.note.x = x;
			this.note.y = y;
			this.note.width = 0;
			this.note.height = 0;
			this.addChildAt(this.note, 22);
			var note_tween:Tween = new Tween(this.note, "width", null, this.note.width, 261, 2, true);
			note_tween = new Tween(this.note, "height", null, this.note.height, 328, 2, true);			
		}
		
		/**
		 * Blendet den Erklärungsbildschirm aus
		 */
		public function hideNote():void
		{
			trace("ist ExplanationNote in hideNOte: " + explanationNote);
			if ((this.mistakes != 0 && this.explanationNote) || !explanationNote){
				var note_tween:Tween = new Tween(this.note, "width", null, this.note.width, 0, 1, true);
				note_tween = new Tween(this.note, "height", null, this.note.height, 0, 1, true);
				note_tween.addEventListener(TweenEvent.MOTION_FINISH, removeNote);
				//var note_tween:Tween = new Tween(this.note, "x", null, this.note.x, 1000, 1, true);	
			}		
		}
		
		private function removeNote(e:Event):void
		{
			this.removeChild(this.note);
		}
		
		/**
		 * Neues Spiel beginnen
		 */ 
		private function playAgain(e:Event):void 
		{
			this.play_btn.enabled = false;
			currentLevel = 0;
			totalPoints = 0;
			this.textField.text = "0";
			textField.setTextFormat(format);  
			hideNote();
			
			cleanUp();
		}
		
		private function buttonPrint(e:Event):void
		{			
			this.print_btn.enabled = false;
			
			this["" + wonMedal + pic].alpha = 1;
			print.printMovieClip( this["" + wonMedal + pic] );
		}
		
		public function cleanUp():void
		{
		//	trace("ist ExplanationNote in cleanUp: " + explanationNote);
		//	if((this.mistakes != 0 && explanationNote) || !explanationNote){
		//		this.removeChild(this.note);
		//	}
			
			this.explanationNote = true;
			this.cart.moveCartFromCurrentXTo(1000, this.setCartBack, true);	
			this.soundChannel = this.squeakSound.play();
			var rect_tween:Tween = new Tween(this.trans_rect, "alpha", null, this.trans_rect.alpha, 0, 2, true);
							
			this.medal_gold.alpha = 0;
			this.medal_bronze.alpha = 0;		
			this.medal_silber.alpha = 0;
			this.medal_gray.alpha = 0;
			this.play_btn.alpha = 0;
			this.print_btn.alpha = 0;
			this.wrongMistakes = new Array();
			this.mistakes = 0;
			this.gameEnd = false;
			this.prodNumber = 0;
			this.catchedProducts = 0;
			this.question = null;
			this.stars = 1;
			this.wonMedal = "";
			
			for (var i:int=1; i < (Config.numberOfMistakes+1); i++){
				this["life"+i].gotoAndStop(1);
			}
			for (var j:int=1; j < (Config.numberOfCatches+1); j++){
				this["wagen"+j].gotoAndStop(1);
			}
			
			this.cart_front.visible = true;
			this.cart_back.visible = true;			
		}
		
		private function setCartBack(e:Event):void
		{
			this.soundChannel.stop();
			this.removeChild(this.cart);
			var tween:Tween = new Tween(this.cart_front, "x", null, this.cart_back.x, Config.cartPositions[0], Config.cartSpeed, false);
			var tween_back:Tween = new Tween(this.cart_back, "x", null, this.cart_back.x, Config.cartPositions[0], Config.cartSpeed, false);
			
			tween.addEventListener(TweenEvent.MOTION_FINISH, setLevel);
			
		}
		
		private function setLevel(e:Event):void
		{
			cartPosition = 1;
			currentLevel++;
			if(currentLevel > 5){
				callScore();
			}else{
				this.gotoAndStop(5);
				this.loadQuestion();	
			}
		}
		
		public function getCurrentLevel():int
		{
			return this.currentLevel;				
		}
	}
}
